package cndw.core.coder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

import cndw.core.dispatch.Request;

/**
 * 用JAVA自带的序列化功能解码
 * @author zuojie.x
 */
public class JavaSerialDecoder implements Decoder {

	@Override
	public Request decode(byte[] bytes) {
		InputStream is = new ByteArrayInputStream(bytes);
		ObjectInputStream ois = null;
		Request request = null;
		try {
			ois = new ObjectInputStream(is);
			request = (Request) ois.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ois != null) ois.close();
				if (is != null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return request;
	}

}
